import { Component, OnInit ,ViewChild } from '@angular/core';
import { CommonService } from '../../service/common.service'
@Component({
  selector: 'app-invites',
  templateUrl: './invites.page.html',
  styleUrls: ['./invites.page.scss'],
})
export class InvitesPage implements OnInit {
  @ViewChild('slidesA') slidesA;

  @ViewChild('copy') copy;
  @ViewChild('atag') atag;

  constructor(private common:CommonService) { }

  public segmentAModel:string = 's1';
  public gift:any;
  public invitelist:any[] = [];
  public firendlist:any[] = [];
  public userInfo:any;
  public ewm:string;
  public iosapp:string = ''
  

  ngOnInit() {
    this.userInfo = this.common.getInfo()
    this.ewm = this.common.configs + '/' +this.userInfo.codePath
    this.iosapp = this.common.configs + '/invite/download.html'
    // this.getAppVersion()
    this.invite()
    // 收益列表
    this.inviteList()
    this.firendList()
  }

  // 切换
  ontabsA(value:any){
    let index:any;
    if(value == 's1'){
      index = 0
    }
    if(value == 's2'){
      index = 1
    }
    if(value == 's3'){
      index = 2
    }
    this.slidesA.slideTo(index,300)
  }
  // 滑动切换
  slideChangeA(){
    this.slidesA.getActiveIndex().then( Number =>{
      // console.log(Number)
      if(Number == 0){
        this.segmentAModel = 's1'
      }
      if(Number == 1){
        this.segmentAModel = 's2'
      }
      if(Number == 2){
        this.segmentAModel = 's3'
      }
      // this.slidesA.el.scrollTop= 0
    })
    // console.log(this.slidesA)
  }


  // 邀请收益
  invite(){
    var api = '/tbr/getEarningsForUser'
    var params = {
      userId:this.common.getInfo().userId
    }
    this.common.post(api,params,false).then((res:any)=>{
      if(res.code == 200){
        this.gift = res.data
      }
    })
  }

  // 收益列表
  inviteList(){
    var api = '/tbr/getEarningsDetailForUser'
    var params = {
      userId:this.common.getInfo().userId,
      page:1,
      limit:500
    }
    this.common.post(api,params,false).then((res:any)=>{
      console.log(res)
      if(res.code == 200){
        this.invitelist = res.data.records
      }
    })
  }

  // 好友列表
  firendList(){
    var api = '/tbr/getMyFriend'
    var params = {
      userId:this.common.getInfo().userId,
      page:1,
      limit:200
    }
    this.common.post(api,params,false).then((res:any)=>{
      // console.log('好友列表')
      // console.log(res)
      if(res.code == 200){
        this.firendlist = res.data.records

        for(var i = 0;i< this.firendlist.length;i++){
          if(this.firendlist[i].avater == '' || this.firendlist[i].avater == null){
            this.firendlist[i].avater="assets/avater.png"
          }
        }

      }
    })
  }

  // 邀请分享
  onSharing(){
    this.atag.nativeElement.href = this.common.configs + '/invite/openshare?invitation_code=' +this.userInfo.code
  }

  // 获取下载地址
  // getAppVersion(){  
  //   var api = '/tbr/getAppVersion'
  //   this.common.get(api).then((res:any)=>{
  //     console.log('获取下载地址',res)
  //     if(res.code == 200){
  //       this.iosapp = res.data.ios_download
  //     }
  //   })
  // }

  // 复制文本
  onCopy(){
    // alert(1)
    this.copy.nativeElement.select()
    // console.log(this.copy)               
    this.common.Clipboard.copy(this.copy.nativeElement.value)
  }


}
